<?php
/**
 * Created by JetBrains PhpStorm.
 * User: yanweidong
 * Date: 13-5-6
 * Time: 下午3:54
 * To change this template use File | Settings | File Templates.
 */
class index extends actionAbstract {


    /**
     * 构造函数
     * @var array
     */
    function __construct() {
        parent::__construct();
        $this->partnerInfo = $this->userinfo=acl::checkLogin('PmsCookie','/index.php/partner/common/login');
        if($this->partnerInfo['parentid']!=0){
        	$this->partnerInfo['id'] = $this->partnerInfo['parentid'];
        }
    }
    //本酒店信息
    function hotelziduan($ziduan){
        $this->loadModel('hotel','category');
        $re=$this->hotel->categoryModel->fetchRow("select $ziduan from hotel_category where id=".(isset($_SESSION['hotel_id'])?$_SESSION['hotel_id']:0)." and partnerid=".$this->partnerInfo['id']);
        if($re){
        	return $re[$ziduan];
        }else{
        	return 0;
        }
    }
    
    /**
     * 报表 首页
     */
    function main(){
        $leftpost="index";
        $this->loadHelper('extend');
    	$this->loadHelper('js');
        $js=new js();
        $str_return = $this->my_authority("index/main");
        if(!$str_return){
        	$js->alert("您没有该权限！");exit();
        }
        $this->loadModel('sys','messages');
        $this->loadModel('pms','login');
        $this->loadModel('pms','orders');
        $hotelId=$this->hotelziduan('id');
        //上次登陆记录
        $time=$this->pms->loginModel->fetchAll("select * from pms_login where pmsid=".$this->partnerInfo['id']." order by logintime desc limit 0,2");
        
        //全部订单
        $total=$this->pms->ordersModel->fetchRow("select count(id) as a from pms_orders where hotelId=$hotelId");
        
        //已完成订单
        $overOrder=$this->pms->ordersModel->fetchRow("select count(id) as a from pms_orders where status=3 and hotelId=$hotelId");
        
        //已取消订单
        $quxiaoOrder=$this->pms->ordersModel->fetchRow("select count(id) as a from pms_orders where status=99 and hotelId=$hotelId");
        
        //待处理订单
        $dcOrder=$this->pms->ordersModel->fetchRow("select count(id) as a from pms_orders where (status=1 or status=0) and hotelId=$hotelId");
        
        //今日新单
        $sta=strtotime(date('Y-m-d',time()));
        $end=strtotime(date('Y-m-d',time()))+24*3600-1;
        $jxOrder=$this->pms->ordersModel->fetchRow("select count(id) as a from pms_orders where (status=0 or status=1) and created>=$sta and created<=$end and hotelId=$hotelId");
        
        //已入住
        $jrOrder=$this->pms->ordersModel->fetchRow("select count(id) as a from pms_orders where status=4 and hotelId=$hotelId");
        
        //今日预离
        $jlOrder=$this->pms->ordersModel->fetchRow("select count(id) as a from pms_orders where endDate>=$sta and endDate<=$end and hotelId=$hotelId and status=4");
        
        //最近30天收入统计
        $allPrice=$this->allth();
        
        include $this->loadWidget('partnerTheme');
    }
    
    function allth(){
        //echo strtotime(date('Y-m-d',time()))-24*3600*10;
        
        $statime=strtotime(date('Y-m-d',time()))-30*24*3600;
        $this->loadModel('pms','orders');
        $hotelId=$this->hotelziduan('id');
        $arr=array();
        for($i=29;$i>=0;$i--){
            $times=$statime+$i*24*3600;
            $sql="select startDate,endDate,countRoom,price from pms_orders where hotelId=$hotelId and startDate<=$times and endDate>$times and (status=3 or status=4)";
            $re=$this->pms->ordersModel->fetchAll($sql);
            $arr[$i]=$re;
        }
        //echo "<pre>";print_r($arr);exit;
        $tharr=array();
        foreach($arr as $k=>$v){
            $tharr[$k]['time']=date('Y-m-d',($statime+$k*24*3600));
            $tharr[$k]['price']=0;
            if($v){
                foreach($v as $key=>$val){
                    $day=($val['endDate']-$val['startDate'])/24/3600;
                    $tharr[$k]['price']+=$val['price']/$day;
                }
            }
        }
        return $tharr;
        //echo "<pre>";print_r($tharr);exit;
    }
    
    /**
     * 查询组合 30天收入的总价数据
     */
    function thPrice($statime){
        $this->loadModel('pms','orders');
        $hotelId=$this->hotelziduan('id');
        //$statime=strtotime(date('Y-m-d',time()))-30*24*3600;
        $arr=array();
        for($i=0;$i<30;$i++){
            $times=$statime+$i*24*3600;
            $sql="select startDate,endDate,countRoom,price from pms_orders where hotelId=$hotelId and startDate<=$times and endDate>$times and (status=3 or status=4)";
            $re=$this->pms->ordersModel->fetchAll($sql);
            $arr[$i]=$re;
        }
        foreach($arr as $k=>$v){
            if($v){
                $arr[$k]=0;
                foreach($v as $key=>$val){
                    $day=($val['endDate']-$val['startDate'])/24/3600;
                    $arr[$k]+=$val['price']/$day;
                }
            }else{
                $arr[$k]=0;
            }
        }
        $html="";
        foreach($arr as $k=>$v){
            if($k==0){
                $html.=$v;
            }else{
                $html.=', '.$v;
            }
        }
        //echo $html;exit;
        return $html;
        //echo "<pre>";print_r($arr);
    }
    
    /**
     * 订单统计
     */
    function orders(){
        $html=$this->thorders();
        exit(json_encode(array('html'=>$html)));
    }
    function thorders(){
        $hotelId=$this->hotelziduan('id');
        $this->loadModel('pms','orders');
        $html="";
        $sql="select * from pms_orders where hotelId=$hotelId";
        $re=$this->pms->ordersModel->fetchAll($sql);
        $dq=0;$dc=0;$yw=0;$yq=0;//待确认、待处理、已完成、已取消
        if($re){
            foreach($re as $k=>$v){
                if($v['status']==0){
                    $dq++;
                }elseif($v['status']==1){
                    $dc++;
                }elseif($v['status']==3){
                    $yw++;
                }elseif($v['status']==99){
                    $yq++;
                }
            }
        }
        $html="[['待确认订单',$dq],['待处理订单',$dc],['已完成订单',$yw],['已取消订单',$yq]]";
        return $html;
    }
    
    /**
     * 今日统计
     */
    function todays(){
        $html=$this->thtodays();
        exit(json_encode(array('html'=>$html)));
    }
    function thtodays(){
        $hotelId=$this->hotelziduan('id');
        $this->loadModel('pms','orders');
        $html="";
        $sql="select * from pms_orders where hotelId=$hotelId";
        $re=$this->pms->ordersModel->fetchAll($sql);
        $jx=0;$jr=0;$jl=0;//今日新单、今日入住、今日预离
        $minTime=strtotime(date('Y-m-d',time()));
        $maxTime=$minTime+24*3600-1;
        if($re){
            foreach($re as $k=>$v){
                if($v['created']>=$minTime&&$v['created']<=$maxTime){
                    $jx++;
                }elseif($v['startDate']>=$minTime&&$v['startDate']<=$maxTime){
                    $jr++;
                }elseif($v['endDate']>=$minTime&&$v['endDate']<=$maxTime){
                    $jl++;
                }
            }
        }
        $html="[['今日新单',$jx],['今日入住',$jr],['今日预离',$jl]]";
        return $html;
    }
    
    /**
     * 柱形表值  统计收入
     */
    function allPrice($sta,$end){
        $hotelId=$this->hotelziduan('id');
        $this->loadModel('pms','orders');
        $this->loadModel('pms','channels');
        $qudao=$this->pms->channelsModel->fetchAll("select * from pms_channels");
        $day=($end-$sta)/24/3600;
        
        $allPrice=array();
        for($i=0;$i<$day;$i++){
            $times=$sta+$i*24*3600;
            if($qudao){
                foreach($qudao as $k=>$v){
                    $sql="select startDate,endDate,countRoom,price from pms_orders where hotelId=$hotelId and startDate<=$times and endDate>$times";
                    $sql.=" and (status=3 or status=4) and type=".$v['id'];
                    $re=$this->pms->ordersModel->fetchAll($sql);
                    $allPrice[$k]['name']=$v['title'];
                    $allPrice[$k]['data'][$i]=$re;
                }
            }
        }
        
        foreach($allPrice as $k=>$v){
            foreach($v['data'] as $ke=>$va){
                $allPrice[$k]['data'][$ke]=0;
                if(is_array($va)){
                    foreach($va as $key=>$val){
                        if($val){
                            $dayth=($val['endDate']-$val['startDate'])/24/3600;
                            $allPrice[$k]['data'][$ke]+=$val['price']/$dayth;
                        }
                    }
                }
            }
        }
        $html="";
        if($allPrice){
            foreach($allPrice as $k=>$v){
                if($k==0){
                    $html.="{name: '".$v['name']."',data:[";
                    foreach($v['data'] as $key=>$val){
                        if($key==0){
                            $html.=$val;
                        }else{
                            $html.=",".$val;
                        }
                    }
                    $html.="]}";
                }else{
                    $html.=", {name: '".$v['name']."',data:[";
                    foreach($v['data'] as $key=>$val){
                        if($key==0){
                            $html.=$val;
                        }else{
                            $html.=",".$val;
                        }
                    }
                    $html.="]}";
                }
            }
        }
        return $html;
        //echo "<pre>";print_r($allPrice);exit;
        //echo $html;exit;
        
        
        /**
         * [{name: '携程旅游',data: [100, 6, 2, 8, 1, 5, 8, 9, 8, 5, 8, 2]}, {name: '同城旅游',data: [10, 0, 5, 11, 17, 22, 24, 24, 20, 14, 8, 2]}],
         */
    }
    
    
	function top(){
        include $this->loadView('top');
    }
	function left(){
        include $this->loadView('left');
    }

	function index(){
	    include $this->loadView('index');
	}

}